package grsu.epam.airline.action;

import grsu.epam.airline.form.AirportForm;
import grsu.epam.airline.service.AirportService;
import grsu.epam.airline.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class AirportUpdateAction extends Action {
	String target = ForwardActionTo.AIRPORTS;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.ADMIN)) {
			AirportForm input = (AirportForm) form;
			AirportService airportService = new AirportService();
			airportService.updateAirport(input.getAirportId(),
					input.getAirportName());
			request.setAttribute("airports", airportService.getAllAirport());
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);
	}
}
